package com.itheima.springbootsecurity.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.springbootsecurity.domain.Article;
import com.itheima.springbootsecurity.domain.LoginUser;
import com.itheima.springbootsecurity.domain.Result;
import com.itheima.springbootsecurity.service.ArticleService;
import com.itheima.springbootsecurity.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author faithwu
 * @since 2024-07-12
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('/article/list')")
    public Result list() {
        LoginUser loginUser = ThreadLocalUtil.get();
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getCreateUser, loginUser.getUser().getId());
        List<Article> list = articleService.list(queryWrapper);

        return Result.success(list);
    }

    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('/article/page')")
    public Result page(@RequestBody Article article) {
        IPage<Article> page = articleService.page(article);

        return Result.success(page);
    }

}
